PROJECT = "httpdemo"
VERSION = "1.0.0"

-- !字体设置：font_opposansm12_chinese
-- sys库是标配
_G.sys = require("sys")
--[[特别注意, 使用http库需要下列语句]]
_G.sysplus = require("sysplus")

---需要在云编译时添加SPI协议
spi_lcd = spi.deviceSetup(2, 7, 0, 0, 8, 40000000, spi.MSB, 1, 1) -- https://wiki.luatos.com/api/spi.html
-- spi.setup(id, cs, CPHA, CPOL, dataw, bandrate, bitdict, ms, mode)

lcd.init("st7789", {
    port = "device",
    pin_dc = 6,
    pin_pwr = 11,
    pin_rst = 10,
    direction = 0,
    w = 240,
    h = 320,
    xoffset = 0,
    yoffset = 0
}, spi_lcd)
-- 如果显示颜色相反，关闭反色
lcd.invoff()

sys.taskInit(function()

    sys.wait(1000)
    log.info(wlan.init())
    wlan.setMode(wlan.STATION)
    wlan.connect("ESP32", "776654321")

    ---等待wifi连接
    while not wlan.ready() do
        log.info("Disconnection")
        sys.wait(1000)
    end
    log.info("ready !!")

    lcd.setFont(lcd.font_opposansm12_chinese) -- 具体取值可参考api文档的常量表
    while 1 do

        ---1.http连接,code为响应代码,body是返回值
        local code, headers, body = http.request("GET",
                                                 "https://api.seniverse.com/v3/weather/now.json?key=SeiP2ViJVtkngR1s5&location=luoyang&language=zh-Hans&unit=c")
                                        .wait() -- 留意末尾的.wait()


        ---2.因为body中有[]，无法对其进行table解析，只能先对body进行字符串操作，去除[]后在进行table解析
        -- print(body)
        -- print(body:sub(13,-3))--!{}号和[]号都算字符串，占位
        API_weather=json.decode(body:sub(13,-3))--!先对body进行去除其他的信息，再将其转换为table
        print(API_weather.location.name)--城市
        print(API_weather.now.text)--当日天气
        print(API_weather.now.temperature)--温度
        print(API_weather.last_update)--更新时间：2023-01-08T20:30:11+08:00

        API_weather_updatetime_year = API_weather.last_update:sub(1, 4) -- 2023
        API_weather_updatetime_month = API_weather.last_update:sub(6, 7) -- 01
        API_weather_updatetime_date  = API_weather.last_update:sub(9, 10) -- 08
        API_weather_updatetime_hour  = API_weather.last_update:sub(12, 13) -- 20
        API_weather_updatetime_minute = API_weather.last_update:sub(15, 16) -- 30
        API_weather_updatetime_second = API_weather.last_update:sub(18, 19) -- 11

        lcd.clear()--!在显示前先进行清屏操作
        lcd.drawStr(15 * 2, 30 * 1, API_weather.last_update)
        lcd.drawStr(15 * 2, 30 * 2, "年: " .. API_weather_updatetime_year) -- !使用汉字必须使固件中有汉字的库
        lcd.drawStr(15 * 2, 30 * 3, "月: " .. API_weather_updatetime_month)
        lcd.drawStr(15 * 2, 30 * 4, "日: " .. API_weather_updatetime_date)
        lcd.drawStr(15 * 2, 30 * 5, "时: " .. API_weather_updatetime_hour)
        lcd.drawStr(15 * 2, 30 * 6, "分: " .. API_weather_updatetime_minute)
        lcd.drawStr(15 * 2, 30 * 7, "秒: " .. API_weather_updatetime_second)

        lcd.drawStr(15 * 2, 30 * 8,API_weather.location.name..API_weather.now.text..API_weather.now.temperature.."摄氏度")
        sys.wait(3000)


    end

end)

-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!
